#include "CImage.h"

CCImage::~CCImage(void)
{
}

CCImage::CCImage( std::string strImgName )
{
	if (this->pSurface != NULL)
	{
		SDL_FreeSurface(this->pSurface);
	}
	this->pSurface = loadImage(strImgName);
}

SDL_Surface * CCImage::loadImage( std::string imgName )
{
	// The image that's loaded 
	SDL_Surface *  loadedImage  =  NULL;

	// The optimized image that will be used 
	SDL_Surface *  optimizedImage  =  NULL;

	// Load the image using SDL_image 
	loadedImage  =  IMG_Load( imgName.c_str() );

	// If the image loaded 
	if ( loadedImage  !=  NULL )
	{
		// Create an optimized image 
		SDL_SetColorKey (loadedImage, SDL_RLEACCEL,
			loadedImage->format->colorkey);
		optimizedImage  =  SDL_DisplayFormatAlpha( loadedImage );

		// Free the old image 
		SDL_FreeSurface( loadedImage );
	} 
	// Return the optimized image 
	return  optimizedImage;
}
